﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using API.Models;
using System.Web.Helpers;
using API.Constant;
using System.Collections;
using System.Net.Http.Headers;
using API.Common;

namespace API.Controllers.Api
{
    public class LocationController : ApiController
    {
        private DataLinqDataContext db = new DataLinqDataContext();
        private Dictionary<string, object> ResponseStatus = new Dictionary<string, object>();
        private Dictionary<string, object> returnData = new Dictionary<string, object>();
        private ReturnStatusConstant returnStatus;

        // GET api/Location
        [HttpGet]
        [ActionName("GetLocation")]
        public HttpResponseMessage GetLocation(int id)
        {
            try
            {
                var checkHeader = APICommon.CheckEtoken(Request.Headers);
                if (checkHeader)
                {
                    var getLesson = (from m in db.tb_Locations
                                     where m.Id == id
                                     select new Location
                                     {
                                         Id = m.Id,
                                         Country = m.Country
                                     }).First();

                    returnStatus = new ReturnStatusConstant(ErrorCode.ErrorCodeNone, Status.Success, MessageResponse.MessageNone, APICommon.Etoken);
                    //.ReturnStatusSuccess, Etoken = APICommon.Etoken };
                    returnData.Add("status", returnStatus);
                    returnData.Add("returnLocation", getLesson);
                    return Request.CreateResponse(HttpStatusCode.Created, returnData);
                }
                else
                {
                    returnStatus =  new ReturnStatusConstant(ErrorCode.ErrorCodeInvalidUserName, Status.Faiure, MessageResponse.MessageInvalidEtoken, string.Empty);
                    returnData.Add("status",returnStatus);
                    return Request.CreateResponse(HttpStatusCode.Created, returnData);
                }
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }
            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // POST api/Location
        [HttpPost]
        [ActionName("AddLocation")]
        public HttpResponseMessage Posttb_Location(tb_Location tb_location)
        {
            HttpResponseMessage response = new HttpResponseMessage();
            Dictionary<string, object> combineResultResponse = new Dictionary<string, object>();
            if (ModelState.IsValid)
            {
                var existLocation = (from m in db.tb_Locations
                                     where m.Country.ToLower() == tb_location.Country.ToLower()
                                     select m).FirstOrDefault();

                if (!string.IsNullOrEmpty(tb_location.Country))
                {
                    if (existLocation == null)
                    {
                        db.tb_Locations.InsertOnSubmit(tb_location);
                        db.SubmitChanges();
                        var returnData = new Location()
                        {
                            Id = tb_location.Id,
                            Country = tb_location.Country
                        };
                        var returnDataST = new ReturnStatusConstant(ErrorCode.ErrorCodeNone,Status.Success,MessageResponse.MessageNone,APICommon.Etoken);
                        combineResultResponse.Add("returnLocation", returnData);
                        combineResultResponse.Add("status", returnDataST);
                        response = Request.CreateResponse(HttpStatusCode.Created, combineResultResponse);
                        response.Headers.Location = new Uri(Url.Link("ApiLocation", new { id = tb_location.Id }));
                    }
                    else
                    {
                        var returnDataST = new ReturnStatusConstant(ErrorCode.ErrorCodeDataExisted, Status.Faiure, MessageResponse.MessageDataExisted, string.Empty);
                        combineResultResponse.Add("status", returnDataST);
                        response = Request.CreateResponse(HttpStatusCode.Created, combineResultResponse);
                        response.Headers.Location = new Uri(Url.Link("ApiLocation", new { id = tb_location.Id }));
                    }
                }
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
    }
}